package com.iseastar.guojiang.newcabinet;

import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.UserBean;
import com.shouhuobao.station.R;

import droid.frame.utils.http.BitmapUtils;

/**
 * Created by joker on 2017/12/23.
 * 火箭侠二维码
 */
public class CourierQRCodeActivity extends BaseActivity2 {

    private ImageView mQRcodeIV;

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_courier_qrcode);
        super.findViewById();
        getAppTitle().setCommonTitle("我的二维码");
        mQRcodeIV = (ImageView) findViewById(R.id.qrcode_iv);
        UserBean userBean = AppCache.getUser();
        ((TextView) findViewById(R.id.user_name_tv)).setText(userBean.getName());
        ((TextView) findViewById(R.id.user_code_tv)).setText("编号：" + userBean.getId());
        if (isNotEmpty(userBean.getQrCodeUrl())) {
            mQRcodeIV.setImageBitmap(BitmapUtils.base64ToBitmap(userBean.getQrCodeUrl()));
        } else {
            showLoadingDialog(null);
            AppHttp.getInstance().getCourierQRCode();
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.courier_get_qrcode:
                cancelLoadingDialog();
                ReqResult<?> result = JSON.parser(msg.obj);
                if (checkLoginStatus(result)) {
                    final String codeStr = (String) result.getResult();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mQRcodeIV.setImageBitmap(BitmapUtils.base64ToBitmap(codeStr));
                            UserBean userBean = AppCache.getUser();
                            userBean.setQrCodeUrl(codeStr);
                            AppCache.setUser(userBean);
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }

    }
}
